import api from '../../../api/index'
import store from '../../../store/index'
const db = wx.cloud.database()
// miniprogram/pages/team/detail/detail.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    team:{
      name: 'KYC.COM',
      currNum: 2,
      needNum: 3,
      introduction:'项目已经有了一定的基础，并且参加过去年的互联网+比赛，团队氛围特别好，进来一起学习',
      contestName:'“创客杯”大学生创业大赛',
      needDatail: '需要一个比较会ps的同学，最好是相关专业的，有相关的创业竞赛的经验就最好啦，主要是不划水  ~',
      createTime: '2020-04-21',
      imgs:[]
    },
    user:{
      avatarUrl: '/images/华农1.jpg',
      nickName: '陈大杰',
    },
    is_my:false,
    my_status:0,
    show: false, // 是否显示”选择队伍”列表,
    tap:false,
    teamID:'',
    clickTrue:false, // 邀请加入 按钮是否可以被点击
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.teamID
    // console.log(id)
    
    // console.log(JSON.parse(options.team))
    this.setData({
      teamID:id
    },()=>{
      this.getTeam()
    })
    // const that = this
    // this.setData({
    //   team:JSON.parse(options.team)
    // },()=>{
    //   var createTime = app.formatDate(this.data.team.createTime)
    //   this.setData({
    //     ["team.createTime"] : createTime
    //   })
    //   that.status().then(res=>{
    //     console.log(this.data.is_my)
    //     console.log(this.data.my_status)
    //   })
    //   that.getTeam()
    // })
  },
  status: async function(){
    return new Promise((reslove,reject)=>{
      var team = this.data.team
      var my_openid = app.globalData.openid
      var mates = this.data.team.mates
      console.log(mates)
      if(team.openid==my_openid){
        this.setData({ 
          is_my:true  //是我的队伍，拥有同意加入等权限
        })
      }
      var myindex = mates.findIndex(item=>{return item.openid == my_openid})
      console.log(myindex)
      if(myindex==-1){
          this.setData({
            my_status:0  //未申请加入，对应button为 ：申请加入
          }) 
      }
      else if(mates[myindex].status=="queue"){
         this.setData({
           my_status:1//申请中，对应button为 ：取消申请
         })  
        }
       else if(mates[myindex].status=="AC"){
          this.setData({
            my_status:2//已经加入，对应button为： 退出队伍
          })
        }
        else if(mates[myindex].status=="WA"){
          this.setData({
            my_status:3//已经加入，对应button为： 退出队伍
          })
        }
      wx.hideLoading()
      reslove("Success")
    })
   
     
  },
  contact: function() {  //跳转到联系界面，但是未传过去双方id
    var id1 =  this.data.team.openid
    var id2 = app.globalData.openid
    wx.navigateTo({
      url: '../../../pages/message/chartRoom/chartRoom?id1='+id1+'&id2='+id2,
    })
  },
  toLookMe: function() {  //查看队长信息，跳转到查看页面
    var openid = this.data.team.openid
    console.log(openid)
    wx.navigateTo({
      url: '/pages/lookMe/lookMe?openid=' + openid,
    })
  },
  cancelRece:function(){
    var contestID = this.data.team.contestID
    if(contestID==undefined){
      wx.showToast({
        title: '没有关联赛事',
        icon:'error'
      })
      return 0
    }
    wx.navigateTo({
      url: '../../contest/detail/detail?id='+contestID,
    })
  },
  checkTap:function(){
    var tap = this.data.tap
    if(tap){
      return 0
    }
    this.setData({
      tap:true
    })
    setTimeout(() => {
      this.setData({
        tap:false
      })
    }, 3500);
    return 1
  },
  apply:function(){  //申请加入队伍
    wx.showToast({
      title: '发送申请成功',
    })
    this.setData({
      clickTrue:true,
    })
   if(!this.checkTap()){
     return 0 
   }

    var nickName = store.data.user.nickName
    if(nickName==''){
      wx.showToast({
        title: '请先登录后使用该功能',
      })
      return 0
    }
    else{
      var teamID = this.data.team._id
      var leaderID = this.data.team.openid
      var params = {teamID,leaderID}
      
      api.team.apply(params).then(res=>{
        console.log(res)
        wx.showToast({
          title: '成功发送申请',
        })
        this.getTeam(teamID)  //刷新加入时的队伍信息
        let pages = getCurrentPages();
        let prevPages = pages[pages.length - 2];
        prevPages.onLoad()
      }).catch(err => {
        console.log(err);
      })
    }
  },
  
  getTeam:function(id){
    var teamID = this.data.teamID
    const that =this
    console.log(teamID)
    db.collection('team').doc(teamID).get().then(res=>{
      console.log(res)
      var createTime = app.formatDate(res.data.createTime)
      res.data.createTime = createTime
      that.setData({
        team:res.data 
      },()=>{
        that.status()
      })
    })
  },
  del:function(){ //取消申请，队伍数量不减一
    if(!this.checkTap()){
      return 0 
    }
    
    var inc = 0;
    var teamID = this.data.team._id
    var params = {teamID,inc}
    api.team.quit(params).then(res=>{
      wx.showToast({
        title: '取消申请成功',
      })
      this.getTeam(teamID)
      let pages = getCurrentPages();
      let prevPages = pages[pages.length - 2];
      prevPages.onLoad()
    })
  },
  quit:function(){
    if(!this.checkTap()){
      return 0 
    }
    
    var inc = -1 //退出队伍，队伍数量减一
    var teamID = this.data.team._id
    var params = {teamID,inc}
    api.team.quit(params).then(res=>{
      wx.showToast({
        title: '退出队伍成功',
      })
      this.getTeam()
      let pages = getCurrentPages();
      let prevPages = pages[pages.length - 2];
      prevPages.onLoad()
    })
  },
  disband:function(){
    var id = this.data.team._id
    var params = {id}
    wx.showModal({
      title: '解散队伍',
      content: '确定要解散本队伍吗',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          api.team.del(params).then(res=>{
            console.log(res)
            wx.showToast({
              title: '解散队伍成功',
              duration:2000

            })
            let pages = getCurrentPages();
            let prevPages = pages[pages.length - 2];
            prevPages.onLoad()
            wx.navigateBack({
              delta: 1,
            })
            wx.hideToast()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
    
    
  }

})